Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support readOnly and writeOnly #547

Merged
merged 1 commit into from Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 25 additions & 18 deletions internal/test/components/components.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions internal/test/components/components.yaml
Expand Up @@ -92,9 +92,20 @@ components:
type: string
firstName:
type: string
readOnlyRequiredProp:
description: |
This property is required and readOnly, so the go model should have it as a pointer,
as it will not be included when it is sent from client to server.
type: string
readOnly: true
writeOnlyRequiredProp:
type: integer
writeOnly: true
required:
- role
- firstName
- readOnlyRequiredProp
- writeOnlyRequiredProp
AdditionalPropertiesObject1:
description: Has additional properties of type int
type: object
Expand Down
10 changes: 8 additions & 2 deletions pkg/codegen/schema.go
Expand Up @@ -67,6 +67,8 @@ type Property struct {
Schema Schema
Required bool
Nullable bool
ReadOnly bool
WriteOnly bool
ExtensionProps *openapi3.ExtensionProps
}

Expand All @@ -76,7 +78,9 @@ func (p Property) GoFieldName() string {

func (p Property) GoTypeDef() string {
typeDef := p.Schema.TypeDecl()
if !p.Schema.SkipOptionalPointer && (!p.Required || p.Nullable) {
if !p.Schema.SkipOptionalPointer &&
(!p.Required || p.Nullable || p.ReadOnly || p.WriteOnly) {

typeDef = "*" + typeDef
}
return typeDef
Expand Down Expand Up @@ -259,6 +263,8 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
Required: required,
Description: description,
Nullable: p.Value.Nullable,
ReadOnly: p.Value.ReadOnly,
WriteOnly: p.Value.WriteOnly,
ExtensionProps: &p.Value.ExtensionProps,
}
outSchema.Properties = append(outSchema.Properties, prop)
Expand Down Expand Up @@ -444,7 +450,7 @@ func GenFieldsFromProperties(props []Property) []string {

fieldTags := make(map[string]string)

if p.Required || p.Nullable || !omitEmpty {
if (p.Required && !p.ReadOnly && !p.WriteOnly) || p.Nullable || !omitEmpty {
fieldTags["json"] = p.JsonFieldName
} else {
fieldTags["json"] = p.JsonFieldName + ",omitempty"
Expand Down